package com.muyu.chat.controller;


import com.muyu.chat.doamin.ChatMessage;
import com.muyu.chat.mapper.MessageMapper;
import com.muyu.chat.service.impl.MessageServiceImpl;
import com.muyu.common.core.domain.Result;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**  控制层
*   JsonResult 为自定义json序列化方法,用自己的方法即可
*  yangle
**/

@RestController
@RequestMapping("/msg")
public class MsgApi {

    @Resource
    private MessageServiceImpl messageService;

    @Resource
    private MessageMapper messageMapper;

    /**
     * 获取好友列表API
     * @return
     */
    @PostMapping("/getFriendMsgList")
    public Result getFriendMsgList(@RequestBody ChatMessage chatMessage) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        Long userId = loginUser.getUserid();
        chatMessage.setSendUserId(userId);
        return Result.success(messageService.getFriendMsgList(userId,chatMessage.getPageNum(),chatMessage.getPageSize()));
    }



    @PostMapping("/getChatMessage")
    public Result getChatMessage(@RequestBody ChatMessage msg) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        msg.setSendUserId(loginUser.getUserid());
        Map<String, Object> chatMessage = messageService.getChatMessage(msg.getSendUserId(), msg.getAcceptUserId(),msg.getPageNum(),msg.getPageSize());
        return Result.success(chatMessage);
    }

    /**
     * 发送消息API
     * @param chatMessage
     * @return
     */

    @PostMapping("sendMsg")
    public Result sendMsg(@RequestBody ChatMessage chatMessage) {
        Map<String,Object> repData = messageService.sendMsg(chatMessage);
        if ((Boolean) repData.get("status"))
        {
            return Result.success(repData.get("returnMsg"));
        }
        return Result.error();
    }
    /**
     * 发送聊天文件API
     * @param acceptUserId
     * @param type
     * @param time
     * @param file
     * @return
     */
    @GetMapping("/sendFileMsg")
    public Result sendFileMsg(    @RequestParam(value = "acceptUserId") Long acceptUserId,
                                  @RequestParam(value = "type") String type,
                                  @RequestParam(value = "time", required = false) Integer time,
                                  @RequestParam(value = "file") MultipartFile file
                                  ) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        Long userId = loginUser.getUserid();
        Long sendUserId = userId;
        Map<String,Object> resData = messageService.sendFileMsg(sendUserId,acceptUserId,type,time,file);
        if ((Boolean) resData.get("status")) {
            return Result.success(resData.get("returnMsg"));
        } else {
            return Result.error("发送失败！");
        }
    }

    /**
     * 已读消息API
     * @param acceptUserId
     * @return
     */

    @GetMapping("/readedMsg")
    public Result readedMsg(@RequestParam("acceptUserId") Long acceptUserId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        Long userId = loginUser.getUserid();
        Long sendUserId = userId;
        if (messageService.readedMsg(sendUserId,acceptUserId)) {
            return Result.success();
        }
        return Result.error(200,"已读失败");
    }

    /**
     * 获取所有消息未读数API
     * @return
     */
    @GetMapping("/getAllNoReadMsgNum")
    public Result getAllNoReadMsgNum() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        Long userId = loginUser.getUserid();
        return Result.success(messageService.getAllNoReadMsgNum(userId));
    }


    /**
     * 删除消息
     * @param chatMessage
     * @return
     */
    @DeleteMapping("/delMsg")
    public Result delMsg(@RequestBody ChatMessage chatMessage ) {
        if (messageMapper.delMsg(chatMessage.getId())) {
            return Result.success("删除消息成功");
        }
        return Result.error("删除消息失败");
    }

}
